from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from .. import models, schemas
from ..crud import friend as friend_crud
from ..database import get_db
from ..deps import get_current_user
from typing import List

router = APIRouter()

@router.post("/requests", response_model=schemas.FriendRequest)
async def create_friend_request(
    request: schemas.FriendRequestCreate,
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """发送好友请求"""
    return friend_crud.create_friend_request(db, current_user.id, request.to_user_id)

@router.get("/requests/received", response_model=List[schemas.FriendRequest])
async def get_received_requests(
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """获取收到的好友请求"""
    return friend_crud.get_friend_requests(db, current_user.id, is_received=True)

@router.get("/requests/sent", response_model=List[schemas.FriendRequest])
async def get_sent_requests(
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """获取发送的好友请求"""
    return friend_crud.get_friend_requests(db, current_user.id, is_received=False)

@router.post("/requests/{request_id}/accept")
async def accept_friend_request(
    request_id: str,
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """接受好友请求"""
    return friend_crud.accept_friend_request(db, request_id, current_user.id)

@router.delete("/requests/{request_id}")
async def reject_friend_request(
    request_id: str,
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """拒绝好友请求"""
    return friend_crud.reject_friend_request(db, request_id, current_user.id)

@router.get("/list", response_model=List[schemas.User])
async def get_friends(
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """获取好友列表"""
    return friend_crud.get_user_friends(db, current_user.id)

@router.delete("/{friend_id}")
async def delete_friend(
    friend_id: str,
    current_user: models.User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """删除好友"""
    return friend_crud.delete_friendship(db, current_user.id, friend_id) 